//
//  SettingsViewController.m
//  Gruphoto
//
//  Created by Thao Nguyen Huy on 1/4/13.
//  Copyright (c) 2013 CNC Software. All rights reserved.
//

#import "SettingsViewController.h"
#import "EventLargeCell.h"
#import "EventPhotoCell.h"
#import "EventPhotoView.h"
#import "EditSettingsViewController.h"
#import "EventDetailViewController.h"

@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
    UIImage *image = [UIImage imageNamed:@"background_navigation_bar.png"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end

@interface SettingsViewController ()

@end

@implementation SettingsViewController
@synthesize userDetailLabels, deviders, detailButtons;
@synthesize eventsTableView, photosTableView;

#pragma mark - View lifecycle
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"background_navigation_bar.png"]
                                                      forBarMetrics:UIBarMetricsDefault];
    }
    self.view.backgroundColor = [UIColor colorWithRed:226/255.0 green:231/255.0 blue:237/255.0 alpha:1.0];
    for (UIView *devider in deviders) {
        if (devider.tag == 1) {
            devider.backgroundColor = [UIColor colorWithRed:129/255.0 green:146/255.0 blue:159/255.0 alpha:1.0];
        }
        else {
            devider.backgroundColor = [UIColor colorWithRed:190/255.0 green:191/255.0 blue:193/255.0 alpha:1.0];
        }
    }
    for (UILabel *label in userDetailLabels) {
        if (label.tag == 0) {
            label.textColor = [UIColor colorWithRed:71/255.0 green:138/255.0 blue:197/255.0 alpha:1.0];
        }
        else {
            label.textColor = [UIColor colorWithRed:107/255.0 green:120/255.0 blue:130/255.0 alpha:1.0];
        }
    }
    [self addBarItems];
    detailType = SettingsDetailTypePosted;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.userDetailLabels = nil;
    self.deviders = nil;
    self.eventsTableView = nil;
    self.photosTableView = nil;
    self.detailButtons = nil;
}

- (void)dealloc {
    [detailButtons release];
    [userDetailLabels release];
    [deviders release];
    [eventsTableView release];
    [photosTableView release];
    [super dealloc];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Private methods
- (void)addBarItems {
    UIImageView *titleImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"title_gruphoto.png"]];
    self.navigationItem.titleView = titleImageView;
    [titleImageView release];
    
    UIButton *editButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [editButton setBounds:CGRectMake(0, 0, 51, 30)];
    [editButton setBackgroundImage:[UIImage imageNamed:@"button_edit.png"] forState:UIControlStateNormal];
    [editButton addTarget:self action:@selector(editButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *editButtonPressed = [[UIBarButtonItem alloc] initWithCustomView:editButton];
    self.navigationItem.rightBarButtonItem = editButtonPressed;
    [editButtonPressed release];
}

#pragma mark - Action methods
- (IBAction)editButtonPressed:(id)sender {
    EditSettingsViewController *editSettingsViewController = [[EditSettingsViewController alloc] initWithNibName:@"EditSettingsViewController" bundle:nil];
    editSettingsViewController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:editSettingsViewController animated:YES];
    [editSettingsViewController release];
}

- (IBAction)detailButtonPressed:(id)sender {
    detailType = [sender tag];
    for (UIButton *button in detailButtons) {
        if (button.tag == detailType) {
            button.enabled = NO;
        }
        else {
            button.enabled = YES;
        }
    }
    switch (detailType) {
        case SettingsDetailTypePosted:
        case SettingsDetailTypeParticipated:
            photosTableView.hidden = YES;
            eventsTableView.hidden = NO;
            [eventsTableView reloadData];
            break;
        case SettingsDetailTypePhotos:
            photosTableView.hidden = NO;
            eventsTableView.hidden = YES;
            break;
        default:
            break;
    }
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *LargeCellId = @"EventLargeCell";
    static NSString *EventPhotoCellId = @"EventPhotoCell";    
    if (tableView == eventsTableView) {
        EventLargeCell *cell = (EventLargeCell *)[tableView dequeueReusableCellWithIdentifier:LargeCellId];
        if (cell == nil) {
            cell = [EventLargeCell cellFromNib];
        }
        cell.eventPassTimeLabel.text = @"7h";        
        if (detailType == SettingsDetailTypePosted) {
            if (indexPath.row > 0) {
                if (indexPath.row % 2 == 0) {
                    cell.eventPassTimeLabel.text = @"Unpublished";
                }
                else {
                    cell.eventPassTimeLabel.text = @"03.01.2013";
                }
            }
        }
        return cell;
    }
    else {
        EventPhotoCell *cell = (EventPhotoCell *)[tableView dequeueReusableCellWithIdentifier:EventPhotoCellId];
        if (cell == nil) {
            cell = [EventPhotoCell cellFromNib];
        }
        for (UIView *holder in cell.placeHolders) {
            CGRect frame = CGRectMake(0, 0, kViewWidth, kViewHeight);
            EventPhotoView *view = [EventPhotoView viewFromNibWithData:nil];
            view.frame = frame;
            view.footerView.hidden = YES;
            [holder addSubview:view];
        }
        return cell;
    }
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == eventsTableView) {
        EventDetailViewController *eventDetailViewController = [[EventDetailViewController alloc] initWithNibName:@"EventDetailViewController" bundle:nil];
        eventDetailViewController.hidesBottomBarWhenPushed = YES;
        if (detailType == SettingsDetailTypePosted) {
            eventDetailViewController.detailMode = EventDetailModeSetting;
        }
        else {
            eventDetailViewController.detailMode = EventDetailModeDetailInSettings;
        }
        eventDetailViewController.eventIndex = indexPath.row;
        [self.navigationController pushViewController:eventDetailViewController animated:YES];
        [eventDetailViewController release];
    }
}

@end
